/*
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.
What is the largest n-digit pandigital prime that exists?

Anser:7652413
Time:50.881623ms
*/
package main

import (
	"fmt"
	"time"
)

const bound = 7654321

var s = [bound]bool{}

func main() {
	tstart := time.Now()
	prime()
	for i := int(bound) - 1; i >= 0; i-- {
		if !s[i] && isUnique(i) {
			fmt.Println(i)
			break
		}
	}
	tend := time.Now()
	fmt.Println(tend.Sub(tstart))
}

// isUnique 判断数字是否唯一
func isUnique(n int) bool {
	m := make(map[int]int)
	d := 0
	for n > 0 {
		m[n%10]++
		d++
		n /= 10
	}
	for k, v := range m {
		if k > d || k == 0 || v > 1 {
			return false
		}
	}
	return true
}

//prime 将切片s下标设置成是否不为质数
func prime() {
	s[0] = true
	s[1] = true
	for i := 2; i < bound; i++ {
		if !s[i] {
			for j := 2; i*j < bound; j++ {
				s[i*j] = true
			}
		}
	}
}
